package com.minshenglife.zanzan.web.weixin;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.pub.utils.Message;
import com.minshenglife.zanzan.result.ReviewResult;
import com.minshenglife.zanzan.service.ReviewService;
import com.minshenglife.zanzan.service.UserService;
import com.minshenglife.zanzan.web.weixin.form.ReviewForm;
import com.minshenglife.zanzan.web.weixin.form.StatForm;
import com.minshenglife.zanzan.web.weixin.service.WXReviewService;
import com.nationsky.pub.utils.StringUtils;


/**
 * 
 * @author huodd
 * 
 */
@Controller
@RequestMapping("/review")
public class ReviewController extends BaseController {
	Logger log = LoggerFactory.getLogger(ReviewController.class);
	@Resource
	ReviewService reviewService;
	@Resource
	UserService userService;
	@Resource
	WXReviewService wxReviewService;
    
    
    /**
     * 实时评论（新鲜事）
     * @param request
     * @param response
     * @param bindForm
     * @return
     */
    @RequestMapping(value="/freshNews.do",method=RequestMethod.POST)
    @ResponseBody
    public Message freshNews(HttpServletRequest request, HttpServletResponse response,
    		StatForm statForm){
    	Message message=null;
    	Page<ReviewResult> page;
		try {
			int pageNo=statForm.getPageNo()==0? 1: statForm.getPageNo();
			int pageSize=statForm.getPageSize()==0? 10: statForm.getPageSize();
			Map<String,Object> map=new HashMap<String,Object>();
			page = reviewService.findReviewInfo(map, pageNo, pageSize);
			message=new Message(Message.STATE_200,"",page);
		} catch (Exception e) {
			e.printStackTrace();
			message=new Message(Message.STATE_one,"服务器异常");
			log.debug(message.toString());
		}
    	 return message;
    }
    /**
     * 我的评论-我评论的
     * @param request
     * @param response
     * @param bindForm
     * @return
     */
    @RequestMapping(value="/myReviews.do",method=RequestMethod.POST)
    @ResponseBody
    public Message myReviews(HttpServletRequest request, HttpServletResponse response,
    		StatForm statForm){
    	Message message=null;
    	Page<ReviewResult> page;
		try {
			String operator=super.getEmpId(request);
			log.debug("当前操作人："+operator);
			int pageNo=statForm.getPageNo()==0? 1: statForm.getPageNo();
			int pageSize=statForm.getPageSize()==0? 10: statForm.getPageSize();
//			Map<String,Object> map=new HashMap<String,Object>();
//			map.put("criticsId", operator);
			page = reviewService.getMyReviewInfo(operator, pageNo, pageSize);
			message=new Message(Message.STATE_200,"",page);
		} catch (Exception e) {
			e.printStackTrace();
			message=new Message(Message.STATE_one,"服务器异常");
			log.debug(message.toString());
		}
    	 return message;
    }
    /**
     * 我的评论-评论我的(评论他人的)
     * @param request
     * @param response
     * @param bindForm
     * @return
     */
    @RequestMapping(value="/reviewMeInfos.do",method=RequestMethod.POST)
    @ResponseBody
    public Message reviewMeInfos(HttpServletRequest request, HttpServletResponse response,
    		StatForm statForm){
    	Message message=null;
    	Page<ReviewResult> page;
		try {
			String operator=super.getEmpId(request);
			log.debug("当前操作人："+operator);
			String empId=operator;
			if(!StringUtils.isNullOrEmpty(statForm.getEmpId())){
				empId=statForm.getEmpId();
			}
			int pageNo=statForm.getPageNo()==0? 1: statForm.getPageNo();
			int pageSize=statForm.getPageSize()==0? 10: statForm.getPageSize();
//			Map<String,Object> map=new HashMap<String,Object>();
//			map.put("byCriticsId", empId);
			page = reviewService.getReviewMeInfo(empId, pageNo, pageSize);
			message=new Message(Message.STATE_200,"",page);
		} catch (Exception e) {
			e.printStackTrace();
			message=new Message(Message.STATE_one,"服务器异常");
			log.debug(message.toString());
		}
    	 return message;
    }
    
    /**
     * 添加评论
     * @param request
     * @param response
     * @param reviewForm
     * @return
     */
    @RequestMapping(value="/addReview.do",method=RequestMethod.POST)
    @ResponseBody
    public Message addReview(HttpServletRequest request, HttpServletResponse response,
    		ReviewForm reviewForm){
    	Message m=null;
    	try {
    		String operator=super.getEmpId(request);
			log.debug("当前操作人："+operator);
			log.debug("被评论人："+reviewForm.getToEmpId());
			log.debug("评论类型："+reviewForm.getCriticsType());
			log.debug("被评论内容："+reviewForm.getContent());
			reviewForm.setFromEmpId(operator);
    		m=wxReviewService.toReview(reviewForm.getContent(), reviewForm.getToEmpId(), reviewForm.getFromEmpId(),reviewForm.getCriticsType());
		} catch (Exception e) {
			e.printStackTrace();
			m=new Message(Message.STATE_one,"服务器异常");
			log.debug(m.toString());
		}
    	return m;
    }
}
